package cn.itsource.celljava.system.register.service.impl;

import cn.itsource.celljava.constants.BaseConstants;
import cn.itsource.celljava.config.BussnisEptional;
import cn.itsource.celljava.system.employee.domain.Employee;
import cn.itsource.celljava.system.employee.mapper.EmployeeMapper;
import cn.itsource.celljava.system.logininfo.domain.Logininfo;
import cn.itsource.celljava.system.logininfo.service.ILogininfoService;
import cn.itsource.celljava.system.register.service.IRegisterService;
import cn.itsource.celljava.system.shop.domain.Shop;
import cn.itsource.celljava.system.shop.mapper.ShopMapper;
import cn.itsource.celljava.system.shop.query.ShopQuery;
import cn.itsource.celljava.utils.HttpUtil;
import cn.itsource.celljava.utils.MD5Utils;
import cn.itsource.celljava.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private HttpUtil httpUtil;
    @Autowired
    private ILogininfoService logininfoService;

    @Override
    public void shopRegister(Shop shop) {
        if (shop.getName()==null){
            throw new BussnisEptional("名称不能为空");
        }
        System.out.println(shop.getAddress());
        if (shop.getAddress()==null || shop.getAddress().equals("")){
            throw new BussnisEptional("地址不能为空");
        }
        if (shop.getTel()==null ||shop.getTel().equals("")){
            throw new BussnisEptional("电话不能为空");
        }
        //获取店长信息
        Employee admin = shop.getAdmin();
        String password = admin.getPassword();
        if (!password.equals(admin.getComfirmPassword())){
            throw new BussnisEptional("两次密码不一致");
        }
        if (!httpUtil.post(shop.getName())){
            throw new BussnisEptional("当前店铺名不合法");
        }
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setKeyword(shop.getName());
        List<Shop> query = shopMapper.findByQuery(shopQuery);
        if (query.size()!=0){
            throw new BussnisEptional("当前店铺名已存在");
        }
        //添加店长状态
        admin.setState(BaseConstants.Employee.STATE_LOCK);
        //对密码进行加密
        String salt = StrUtils.getRandomString(128);
        String md5Password = MD5Utils.encrypByMd5(password + salt);
        //将添加后的密码和盐值存入店长数据
        admin.setSalt(salt);
        admin.setPassword(md5Password);
        //将店长信息添加到logininfo表中
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(BaseConstants.Type.STATE_USER);
        logininfo.setDisable(BaseConstants.User.STATE_LOGOUT);
        logininfoService.add(logininfo);
        //获取logininfo的id
        Long id = logininfo.getId();
        admin.setLogininfoId(id);
        //添加店长信息
        employeeMapper.add(admin);
        //获取店长id
        Long adminId = admin.getId();
        //保存店铺信息
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);
        //添加店长id到店铺信息中
        shop.setAdminId(adminId);
        shopMapper.add(shop);
        //获取店铺id
        Long shopId = shop.getId();
        //添加店铺id到店长信息中
        admin.setShopId(shopId);
        employeeMapper.update(admin);
    }
}
